//
//  VipPrivilegeVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/10.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class VipPrivilegeVC: BaseViewController{
    
    var dataModel : VipPrivilegeModel?
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: true)
        
        
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        
//        plainTableView.register(BaseTableViewCell.self, forCellReuseIdentifier: BaseTableViewCell.identifer())
        
        automaticallyAdjustsScrollViewInsets = false
//        
        
        plainTableView.tableFooterView = UIView()
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: VipPrivTopCell.self), bundle: nil), forCellReuseIdentifier: VipPrivTopCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: VipPrivCommentCell.self), bundle: nil), forCellReuseIdentifier: VipPrivCommentCell.identifer())
        
        
        weak var weakSelf = self
        
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }

         plainTableView.mj_header.beginRefreshing()
        // Do any additional setup after loading the view.
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        SVProgressHUD.dismiss()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   
}


extension VipPrivilegeVC{

    func loadData() {
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_index&a=welfare", parameters: nil) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                let temp = responseObject?["datas"]?["member_info"] as! [String : AnyObject]
                
                self!.dataModel = VipPrivilegeModel(dic: temp)
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
            
        }
    }




}




// MARK:- tableview代理方法
extension VipPrivilegeVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
      
        return 1
            
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
        return 4
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        var tableCell : UITableViewCell? = nil
        
        switch indexPath.row {
        case 0:
            
                let tempCell : VipPrivTopCell =  tableView.dequeueReusableCell(withIdentifier: VipPrivTopCell.identifer()) as! VipPrivTopCell
                tempCell.model = dataModel
                tableCell = tempCell
                
        default:
            
        let tempCell : VipPrivCommentCell =  tableView.dequeueReusableCell(withIdentifier: VipPrivCommentCell.identifer()) as! VipPrivCommentCell
        
        tempCell.index = indexPath
        
        tempCell.model = dataModel
        
        tempCell.delegate = self

        tableCell = tempCell
            
            
        }
        
        
        return tableCell!

    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.row == 0 {
            return 300
        } else if indexPath.row == 1{
            return 172
        }else{
            return 110

        }
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
}


}



extension VipPrivilegeVC : VipPrivCommentCellDeleage{


    func rightBtnDidTap(str: String) {
        
        if str == "立即邀请" {
            
            shareDidClick()
            
        } else if str == "马上签到" {
            
            signin()
            
        }else{
        
        
        
        }
        
        
    }
    
    
    
    
    
    func signin(){
        
        NetworkTools.shared.sendRequest(method: .POST_METHOD, pathStr: "mobile/index.php?c=member_signin&a=signin_add", parameters: nil) {[weak self] (responseObject, error) in
            
            if error == nil{
                
                if let temp = responseObject?["datas"]?["point"]{
                    
                     self!.dataModel?.bean =  temp as! String
                
                
                }
                
                self!.dataModel?.can_signin = "0"
                
                
                let str = responseObject?["msg"] as? String
                
                if str != nil{
                
                    SVProgressHUD.showSuccess(withStatus: str)

                
                }
            
        
                
                  self!.plainTableView.reloadData()
                
                
            }else{
            
              SVProgressHUD.showError(withStatus: error?.domain)
            
            }
            
        }
    }



}





extension VipPrivilegeVC{


    func shareDidClick() {
        
        weak var weakSelf = self
        
        if dataModel?.share_data?.image == nil {
            QL1("分享")
            dataModel?.share_data?.image = "http://pic.58pic.com/58pic/13/19/68/14g58PICJdQ_1024.jpg"
            
        }
        
        
        
        UMSocialUIManager.showShareMenuViewInWindow(platformSelectionBlock: { (platformType, userInfo) in
            
            switch platformType {
            case .wechatSession:
                weakSelf?.share(platformType: .wechatSession, title: weakSelf!.dataModel?.share_data?.title, desc: weakSelf!.dataModel?.share_data?.desc, image: weakSelf!.dataModel?.share_data?.image, url: weakSelf!.dataModel?.share_data?.url)
            case .wechatTimeLine:
                weakSelf?.share(platformType: .wechatTimeLine, title: weakSelf!.dataModel?.share_data?.title, desc: weakSelf!.dataModel?.share_data?.desc, image: weakSelf!.dataModel?.share_data?.image, url: weakSelf!.dataModel?.share_data?.url)
            case .QQ:
                weakSelf?.share(platformType: .QQ, title: weakSelf!.dataModel?.share_data?.title, desc: weakSelf!.dataModel?.share_data?.desc, image: weakSelf!.dataModel?.share_data?.image, url: weakSelf!.dataModel?.share_data?.url)
            case .qzone:
                weakSelf?.share(platformType: .qzone, title: weakSelf!.dataModel?.share_data?.title, desc: weakSelf!.dataModel?.share_data?.desc, image: weakSelf!.dataModel?.share_data?.image, url: weakSelf!.dataModel?.share_data?.url)
            default:
                weakSelf?.share(platformType: .wechatSession, title: weakSelf!.dataModel?.share_data?.title, desc: weakSelf!.dataModel?.share_data?.desc, image: weakSelf!.dataModel?.share_data?.image, url: weakSelf!.dataModel?.share_data?.url)
            }
            
            
            
        })
        
        
        
        
    }
    
    
    
    func share(platformType : UMSocialPlatformType,title : String?,desc : String?,image : String?,url : String?) {
        
        let sharTitle = title ?? ""
        
        let sharDesc = desc ?? ""
        
        let sharImage = image ?? "http://pic.58pic.com/58pic/13/19/68/14g58PICJdQ_1024.jpg"
        
        let sharUrl = url ?? ""
        
        
        let messageObj = UMSocialMessageObject.init()
        
        let shareObj = UMShareWebpageObject.shareObject(withTitle: sharTitle, descr: sharDesc, thumImage: sharImage)
        
        shareObj?.webpageUrl = sharUrl
        
        messageObj.shareObject = shareObj
        
        
        UMSocialManager.default().share(to: platformType, messageObject: messageObj, currentViewController: self) { (resp, error) in
            
            if error == nil{
                
                let temp = resp as? UMSocialShareResponse
                
                QL1(temp?.message)
                
                QL1(temp?.originalResponse)
                
            }
            
            
        }
        
        
    }




}
